package com.yxh.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;

import com.yxh.R;
import com.yxh.dto.LessonDto;
import com.yxh.dto.UserDto;
import com.yxh.fragment.AboutMeFragment;
import com.yxh.fragment.ClassFragment;
import com.yxh.fragment.HomeFragment;
import com.yxh.service.DataService;
import com.yxh.service.Task;
import com.yxh.view.TabFragmentHost;

public class MainActivity extends BaseActivity {

	public TabFragmentHost mTabHost;
	private FragmentManager mFragmentManager;
	// 标签
	private String[] TabTag = { "tab1", "tab2", "tab3" };
	// 自定义tab布局显示文本和顶部的图片
	private Integer[] ImgTab = { R.layout.tab_main_home,
			R.layout.tab_main_class, R.layout.tab_main_my };

	// tab 选中的activity
	@SuppressWarnings("rawtypes")
	private Class[] ClassTab = { HomeFragment.class, ClassFragment.class,
			AboutMeFragment.class };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setupView();
		initValue();
		setLinstener();
		fillData();

	}

	private void setupView() {
		// 实例化framentTabHost
		mTabHost = (TabFragmentHost) findViewById(android.R.id.tabhost);
		mFragmentManager = getSupportFragmentManager();

		mTabHost.setup(this, mFragmentManager, android.R.id.tabcontent);

	}

	private void initValue() {

		// 初始化tab选项卡
		InitTabView();
	}

	private void setLinstener() {
		// imv_back.setOnClickListener(this);

	}

	private void fillData() {
		// TODO Auto-generated method stub

	}

	private void InitTabView() {
		// 可以传递参数 b;传递公共的userid,version,sid
		Bundle b = new Bundle();
		// 循环加入自定义的tab
		for (int i = 0; i < TabTag.length; i++) {
			// 封装的自定义的tab的样式
			View indicator = getIndicatorView(i);
			mTabHost.addTab(
					mTabHost.newTabSpec(TabTag[i]).setIndicator(indicator),
					ClassTab[i], b);// 传递公共参数

		}
		mTabHost.getTabWidget().setDividerDrawable(R.color.white);
	}

	// 设置tab自定义样式:注意 每一个tab xml子布局的linearlayout 的id必须一样
	private View getIndicatorView(int i) {
		// 找到tabhost的子tab的布局视图
		return getLayoutInflater().inflate(ImgTab[i], null);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void refresh(Object... param) {
		try {
			int operType = (Integer) param[0];
			Object obj = param[1];
			switch (operType) {

			case Task.GET_LESSON_LIST:
				closeDialog();
				LessonDto dto = (LessonDto) obj;
				if (dto != null) {
					if (DataService.DATA_SUCCESS.equals(dto.result)) {
						Fragment fragement = mFragmentManager
								.findFragmentByTag(TabTag[0]);
						if (fragement instanceof HomeFragment)
							((HomeFragment) fragement).refreshHomeData(dto);
					} else {
						showToast(dto.message);
					}
				} else {
					// ???
					showToast(getString(R.string.http_no_net_tip));
				}

				break;

			case Task.GET_MY_LESSON_FRAGMENT:
				closeDialog();

				if (obj != null) {
					List<LessonDto> li = (ArrayList<LessonDto>) obj;
					if ("1".equals(li.get(0).result)) {
						Fragment fragement = mFragmentManager
								.findFragmentByTag(TabTag[1]);
						if (fragement instanceof ClassFragment)
							((ClassFragment) fragement).refurshData(li);
					} else {
						Fragment fragement = mFragmentManager
								.findFragmentByTag(TabTag[1]);
						if (fragement instanceof ClassFragment)
							((ClassFragment) fragement)
									.loadingLayoutShowEmpty();
						showToast(li.get(0).message);
					}

				} else {
					Fragment fragement = mFragmentManager
							.findFragmentByTag(TabTag[1]);
					if (fragement instanceof ClassFragment)
						((ClassFragment) fragement).loadingLayoutShowError();
					showToast(getString(R.string.http_no_net_tip));
				}

				break;

			case Task.ADD_LESSON_CLASS_FRAGMENT:
				if (obj != null) {
					List<LessonDto> li = (ArrayList<LessonDto>) obj;
					if ("1".equals(li.get(0).result)) {
						Fragment fragement = mFragmentManager
								.findFragmentByTag(TabTag[1]);
						if (fragement instanceof ClassFragment)
							((ClassFragment) fragement).addClassItem();
					} else {
						closeDialog();
						showToast(li.get(0).message);
					}

				} else {
					closeDialog();
					showToast(getString(R.string.http_no_net_tip));
				}
				break;

			case Task.CANCEL_LESSON_FRAGMENT:

				if (obj != null) {
					UserDto uDto = (UserDto) obj;
					if ("1".equals(uDto.result)) {
						Fragment fragement = mFragmentManager
								.findFragmentByTag(TabTag[1]);
						if (fragement instanceof ClassFragment)
							((ClassFragment) fragement)
									.removeClassItem(uDto.uid);
					} else {
						closeDialog();
						showToast(uDto.message);
					}

				} else {
					closeDialog();
					showToast(getString(R.string.http_no_net_tip));
				}
				break;

			default:
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** 双击退出函数 **/
	private static Boolean isExit = false;

	public void exitBy2Click() {
		Timer tExit = null;
		if (isExit == false) {
			isExit = true; // 准备退出
			showToast("再按一次退出程序");
			tExit = new Timer();
			tExit.schedule(new TimerTask() {
				@Override
				public void run() {
					isExit = false; // 取消退出
				}
			}, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

		} else {
			finish();
			System.exit(0);
		}
	}

}
